import {
  getUpGoodsTable,
  getUpGoodsInfoTable,
  editUpGoodsRecord
} from '../../api/task'
import {
  UP_GOODS_LIST,
  UP_GOODS_INFO_LIST,
  UP_GOODS_CURRENT_ROW,
  UP_GOODS_DATA_COUNT,
  UP_GOODS_HISTORY_DATA,
  UP_GOODS_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    upGoodsList: [],
    upGoodsInfoList: [],
    upGoodsDataCount: null,
    upGoodsPageSize: 20,
    upGoodsHistoryData: [],
    row: {
      uid: '',
      uiname: '',
      ctime: '',
      state: '',
      names: '',
      gname: '',
      gid: '',
      num: '',
      code: '',
      tel: ''
    }
  },
  getters: {
    upGoodsList: state => state.upGoodsList
  },
  mutations: {
    [UP_GOODS_LIST] (state, data) {
      state.upGoodsList = data
    },
    [UP_GOODS_INFO_LIST] (state, data) {
      state.upGoodsInfoList = data
    },
    [UP_GOODS_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [UP_GOODS_DATA_COUNT] (state, data) {
      state.upGoodsDataCount = data
    },
    [UP_GOODS_HISTORY_DATA] (state, data) {
      state.upGoodsHistoryData = data
    },
    [UP_GOODS_PAGE_SIZE] (state, data) {
      state.upGoodsPageSize = data
    }
  },
  actions: {
    async getUpGoodsList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getUpGoodsTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          uid: item.uid,
          gid: item.gid,
          uiname: item.uiname,
          ctime: item.ctime,
          state: item.state,
          names: item.names,
          gname: item.gname,
          num: item.num,
          code: item.code,
          tel: item.tel
        }]
      })
      commit(UP_GOODS_LIST, newData)
      commit(UP_GOODS_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.upGoods.upGoodsPageSize) {
        commit(UP_GOODS_HISTORY_DATA, newData)
      } else {
        commit(UP_GOODS_HISTORY_DATA, newData.slice(0, rootState.upGoods.upGoodsPageSize))
      }
      return resp
    },
    async getUpGoodsInfoList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getUpGoodsInfoTable(data)
      // console.log(resp)
      newData = resp.data.data
      commit(UP_GOODS_INFO_LIST, newData)
      return resp
    },
    async editUpGoodsData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editUpGoodsRecord(data)
      return resp
    },
    async getUpGoodsCurrentRow ({ commit, rootState }, row) {
      commit(UP_GOODS_CURRENT_ROW, row)
    },
    async changeUpGoodsPage ({ commit, rootState }, index) {
      let data = rootState.upGoods.upGoodsList
      let _start = (index - 1) * rootState.upGoods.upGoodsPageSize
      let _end = index * rootState.upGoods.upGoodsPageSize
      commit(UP_GOODS_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeUpGoodsPageSize ({ commit, rootState }, value) {
      commit(UP_GOODS_PAGE_SIZE, value)
    }
  }
}
